package com.eas.ereceipt.network;

import android.content.Context;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class RestConnector {
	
	private static String IP = "54.243.244.187:8090";
	public static String BASE_API_URL = "http://" + IP + "/Services/";
	
	
	private static int getTimeOut = NetworkUtility.DEFAULT_GET_TIME_OUT;
	private static int postTimeOut = NetworkUtility.DEFAULT_POST_TIME_OUT;
	
	private static AsyncHttpClient client = new AsyncHttpClient();

	public static void post(Context c, String url, RequestParams params,
			AsyncHttpResponseHandler response) {
		client.setTimeout(getTimeOut);
		client.post(c, getAbsoluteUrl(url), params, response);
		Log.e("RestConnector", "" + getAbsoluteUrl(url));
		Log.e("RestConnector", "param" + params);
	}
	
	public static void postFile(Context c, String url, RequestParams params,
			AsyncHttpResponseHandler response) {
		client.setTimeout(postTimeOut);
		client.post(c, getAbsoluteUrl(url), params, response);
		Log.e("RestConnector", "" + getAbsoluteUrl(url));
		Log.e("RestConnector", "param" + params);
	}

	public static void get(Context c, String url, RequestParams params,
			AsyncHttpResponseHandler responseHandler) {
		client.setTimeout(getTimeOut);
		client.get(c, getAbsoluteUrl(url), params, responseHandler);
	}

	public static void cancelRequest(Context c) {
		client.cancelRequests(c, true);
	}

	public static void setGetTimeOut(int timeOut) {
		RestConnector.getTimeOut = timeOut;
	}
	
	public static void setPostTimeOut(int timeOut) {
		RestConnector.postTimeOut = timeOut;
	}
	
	private static String getAbsoluteUrl(String relativeUrl) {
		return new String(BASE_API_URL + relativeUrl);
	}

	public static void resetURL() {
		BASE_API_URL = "http://" + IP + "/Services/";
	}

}
